/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pdfManager;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.JPanel;

/**
 *
 * @author Allan
 */
public abstract class PdfManager
{
    public static void createPDF(String mappeSti, String kundeTelefon, int ordreNr, JPanel documentPanel) throws BadElementException, DocumentException, FileNotFoundException, MalformedURLException, IOException, Exception
    {   
        File f = new File(mappeSti);
        
        boolean result;
        result = false;
        
        // Set folder name
        String aktuelMappe;
        aktuelMappe = f.getName();
        
        // Set pdf name
        String pdfName;
        pdfName = aktuelMappe + "_" + kundeTelefon + "_" + ordreNr + ".pdf";

        // Set path to pdf
        String pdfPath;
        pdfPath = mappeSti + "\\" + pdfName;

        // Create pdf from panel
        result = pdfManager.PanelToPDF.createPdfFromPanel(documentPanel, pdfPath);
        
        // Call method to open PDF
        if(result)
        {
            openPDF(pdfPath);
        }
    }
    
    private static void openPDF(String pdfPath) throws IOException, Exception
    {
        // See if pdf exists
        File pdfExisting = new File(pdfPath);
        if(pdfExisting.exists())
        {
            // Try to open pdf in local default pdfviewer
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfExisting);
        }
    }
}
